package com.speechb.common.local;

import java.util.HashMap;
import java.util.Map;

/**
 * 用来保存当前线程的数据
 */
public class Local {
	/**
	 * 父类子类都共享数据
	 */
	private static InheritableThreadLocal<Local> threadLocal = new InheritableThreadLocal<Local>();
	/**
	 * 渲染成json数据使用的Map
	 */
	private Map<Object, Object> currentThreadMap;
	/**
	 * 存在当前线程，不渲染成json的map
	 */
	private Map<Object, Object> extraThreadMap ;
	private Local() {
	};

	/**
	 * 获得当前线程对应local对象
	 * @return
	 */
	public static synchronized Local getThreadLocal() {
		Local currentThreadLocal = threadLocal.get();
		if (currentThreadLocal == null) {
			currentThreadLocal = new Local();
			threadLocal.set(currentThreadLocal);
		}
		return currentThreadLocal;
	}

	public static  synchronized void putExtra(Object key, Object value) {
		getExtraCurrentThreadMap().put(key, value);
	}

	/**
	 * 将数据放入要渲染成json的map中
	 * @param key
	 * @param value
	 */
	public static  synchronized void put(Object key, Object value) {

		getCurrentThreadMap().put(key, value);
	}
	public static Object get(Object key) {
		return getCurrentThreadMap().get(key);
	}

	/**
	 * 得到不渲染成json的map
	 * @return
	 */
	public static Map  getExtraCurrentThreadMap(){
		if(getThreadLocal().extraThreadMap==null){
			getThreadLocal().extraThreadMap=new HashMap();
		}
		return getThreadLocal().extraThreadMap;
	}

	/**
	 * 得到可渲染成json的map
	 * @return
	 */
	public static Map getCurrentThreadMap() {
		if(getThreadLocal().currentThreadMap==null){
			getThreadLocal().currentThreadMap=new HashMap();
		}
		return getThreadLocal().currentThreadMap;
	}
}
